package com.mcflies.commons.action;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import com.mcflies.commons.commons.ActionReturnConstantKey;
import com.mcflies.commons.commons.CommonsConstantKey;
import com.mcflies.commons.util.CheckCodeImgTool;
import com.opensymphony.xwork2.ActionContext;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 
 * CheckCodeAction.java
 * @describe：获取验证码图片Action类
 * @Copyright(c) 2012-2024 Study,mcfly
 * @author mcfly_jun
 */
public class CheckCodeAction {
	private InputStream checkCodeImg;
	
	public String getCheckCode(){
		Map<String, BufferedImage> imageMap = CheckCodeImgTool.getSimpleImage();
		String checkCode = imageMap.keySet().iterator().next();
		BufferedImage image = imageMap.get(checkCode);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(baos);
		try {
			imageEncoder.encode(image);
			checkCodeImg = new ByteArrayInputStream(baos.toByteArray());
			ActionContext.getContext().getSession().put(CommonsConstantKey.EXTJS_LOGION_CHECK_CODE_SESSION_KEY, checkCode);
			return ActionReturnConstantKey.SUCCESS;
		} catch (IOException e) {
			e.printStackTrace();
			return ActionReturnConstantKey.FAIL;
		}
	}

	public InputStream getCheckCodeImg() {
		return checkCodeImg;
	}

	public void setCheckCodeImg(InputStream checkCodeImg) {
		this.checkCodeImg = checkCodeImg;
	}
	
}
